package com.cc.openeye.dynamic.recommend.items.square

import androidx.compose.runtime.Composable
import com.cc.openeye.backgroundColor
import com.cc.openeye.borderRadius
import com.cc.openeye.dynamic.recommend.entity.dynamicResponse.ItemX
import com.cc.openeye.height
import com.tencent.kuikly.compose.coil3.rememberAsyncImagePainter
import com.tencent.kuikly.compose.foundation.Image
import com.tencent.kuikly.compose.foundation.layout.Arrangement
import com.tencent.kuikly.compose.foundation.layout.Box
import com.tencent.kuikly.compose.foundation.layout.BoxWithConstraints
import com.tencent.kuikly.compose.foundation.layout.Column
import com.tencent.kuikly.compose.foundation.layout.Row
import com.tencent.kuikly.compose.foundation.layout.fillMaxHeight
import com.tencent.kuikly.compose.foundation.layout.fillMaxWidth
import com.tencent.kuikly.compose.foundation.layout.padding
import com.tencent.kuikly.compose.material3.Text
import com.tencent.kuikly.compose.ui.Alignment
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.graphics.Color
import com.tencent.kuikly.compose.ui.text.font.FontWeight
import com.tencent.kuikly.compose.ui.unit.dp
import com.tencent.kuikly.compose.ui.unit.sp

@Composable
fun SquareItem(itemList: List<ItemX>) {
    AdaptiveGrid(
        items = itemList,
        columns = 2,
    ) { dataItem ->

        BoxWithConstraints(
            modifier = Modifier.fillMaxWidth().height(90f)
                .backgroundColor(Color(0xfff5f5f5)).borderRadius(5.dp),
            contentAlignment = Alignment.TopCenter
        ) {
            Image(
                modifier = Modifier.fillMaxWidth().fillMaxHeight(),
                painter = rememberAsyncImagePainter(dataItem.data?.image),
                contentDescription = "",
            )


            Text(
                dataItem.data?.title ?: "",
                fontSize = 16.sp,
                color = Color.White,
                modifier = Modifier.padding(top = 20.dp),
                fontWeight = FontWeight.W700
            )

            Text(
                dataItem.data?.subTitle ?: "",
                fontSize = 14.sp,
                color = Color.White,
                modifier = Modifier.padding(bottom = 20.dp).align(Alignment.BottomCenter),
            )

        }

    }
}

@Composable
fun AdaptiveGrid(
    items: List<ItemX>,
    columns: Int,
    modifier: Modifier = Modifier,
    content: @Composable (ItemX) -> Unit
) {
    val rows = (items.size + columns - 1) / columns
    Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(5.dp)) {
        for (rowIndex in 0 until rows) {
            val startIndex = rowIndex * columns
            val endIndex = minOf(startIndex + columns, items.size)
            val currentRowItems = items.subList(startIndex, endIndex)
            Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) {
                for (item in currentRowItems) {
                    Box(
                        modifier = Modifier.weight(1f)
                    ) {
                        content(item)
                    }
                }
                // 如果当前行不够列数，用空的Box填充保持对齐
                if (currentRowItems.size < columns) {
                    for (i in currentRowItems.size until columns) {
                        Box(modifier = Modifier.weight(1f)) {}
                    }
                }
            }
        }
    }
}